<template>
  <div class="login_main">
    <div class="login_form">
      <div class="login_logo">
        <img src="@/image/logo.png" alt="" />
      </div>
      <div class="login_message">
        <div class="form_phone">
          <div class="phone_label">手机号</div>
          <a-input
            v-model:value="formInline.phone"
            placeholder="请输入您的手机号"
          />
          <div class="phone_verification" @click="phoneSubmit">
            <span v-show="!phoneStatu.phoneLoading">{{ phoneStatu.text }}</span>
            <a-spin v-show="phoneStatu.phoneLoading" />
          </div>
        </div>
        <div class="form_code">
          <div class="form_code_message">
            <div class="phone_label">验证码</div>
            <a-input
              v-model:value="formInline.captcha"
              placeholder="请输入您收到的验证码"
            />
          </div>
        </div>
        <a-button @click="onSubmit" :loading="submitLoading">登录</a-button>
      </div>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent, reactive, ref } from "vue";
import { captcha, login } from "@/utils/fetch/api";
import { message, notification } from "ant-design-vue";
import { useRouter } from "vue-router";
export default defineComponent({
  name: "login",

  components: {},

  setup() {
    const submitLoading = ref(false);
    const phoneStatu = reactive({
      phoneLoading: false,
      text: "点击验证",
    });

    const formInline = reactive({
      phone: "",
      captcha: "",
    });

    //手机号
    const phoneSubmit = async () => {
      phoneStatu.phoneLoading = true;
      if (!/^1[3456789]\d{9}$/.test(formInline.phone)) {
        message.warning("请输入正确的手机号！");
      } else {
        try {
          const data = await captcha(formInline);
          notification["info"]({
            message: "已发送",
            description: "已发送验证码到您的手机上，请注意查收",
            class: "ant_message",
          });
          phoneStatu.text = "已发送";
        } catch (err) {
          console.error(err);
        }
      }
      phoneStatu.phoneLoading = false;
    };

    const router = useRouter();
    //login
    const onSubmit = async () => {
      submitLoading.value = true;
      if (formInline.phone.length <= 0) {
        message.warning("请验证手机号！");
        submitLoading.value = false;
        return
      } else if (formInline.captcha.length <= 0) {
        message.warning("请输入验证码！");
        submitLoading.value = false;
        return
      }
      try {
        const data = await login(formInline);
        console.log(data);
        if (data.errorCode != "200") {
          message.error("验证码错误，请重新验证！");
        } else {
          const token = data.data.token;
          const tokenTime: any = new Date().getTime();
          localStorage.setItem("token", token);
          localStorage.setItem("tokenStartTime", tokenTime);

          localStorage.setItem("userMessage", formInline.phone);

          router.push("/home");
        }
      } catch (err) {
        console.error(err);
      } finally {
        submitLoading.value = false;
      }
    };
    return {
      formInline,
      phoneSubmit,
      onSubmit,
      submitLoading,
      phoneStatu,
    };
  },
});
</script>
<style lang="less" scope>
.login_main {
  width: 100%;
  height: 100%;
  background: url("@/image/login.webp") no-repeat;
  background-position: center;
  background-size: cover;
  position: relative;
  .login_form {
    width: 100%;
    height: 100%;
    background: #0000007d;
    .login_logo {
      img {
        margin-top: -7em;
        width: 50em;
      }
    }
    .login_message {
      width: 40em;
      height: 20em;
      position: absolute;
      left: calc(50% - 20em);
      top: 40em;
      display: flex;
      flex-direction: column;
      align-items: center;
      .form_phone {
        width: 80%;
        height: 3em;
        background: white;
        border-radius: 20px;
        display: flex;
        align-items: center;
        justify-content: space-between;
        .phone_label {
          width: 5em;
          background: #ff8033;
          height: 100%;
          border-radius: 20px;
          display: flex;
          align-items: center;
          justify-content: space-evenly;
          color: white;
          font-size: 1.2em;
        }
        .ant-input {
          width: 55%;
          border: 0;
        }
        .ant-input:focus {
          border: 0;
          box-shadow: 0 0 0;
        }
        .phone_verification {
          width: 7em;
          background: #362a2a29;
          height: 100%;
          border-radius: 20px;
          display: flex;
          align-items: center;
          justify-content: space-evenly;
          color: black;
          font-size: 1.2em;
          margin-right: -2px;
          cursor: pointer;
          .ant-spin {
            margin-top: 6px;
          }
        }
        .phone_verification:hover {
          background: #65615554;
        }
        .a-loading-mask {
          border-radius: 20px !important;
        }
      }

      .form_code {
        width: 80%;
        height: 3em;
        margin-top: 40px;
        .form_code_message {
          width: 80%;
          height: 100%;
          background: white;
          border-radius: 20px;
          display: flex;
          align-items: center;
          justify-content: flex-start;
        }
        .phone_label {
          width: 5em;
          background: #ff8033;
          height: 100%;
          border-radius: 20px;
          display: flex;
          align-items: center;
          justify-content: space-evenly;
          color: white;
          font-size: 1.2em;
        }
        .ant-input {
          width: 75%;
          border: 0;
        }
        .ant-input:focus {
          border: 0;
          box-shadow: 0 0 0;
        }
      }

      .ant-btn {
        background: #ff8033;
        border: 0;
        width: 5em;
        height: 2.5em;
        border-radius: 20px;
        color: white;
        font-size: 1.2em;
        margin-top: 40px;
      }
    }
  }
}

.ant-message-notice {
  border-radius: 20px;
}
</style>
